Third sub title
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(tmap)
library(leafpop)
library(leaflet)
library(tmaptools)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.4 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
setwd("..")
OSM <- st_read(here::here( "wk5/prac5_data",
"greater-london-latest-free",
"gis_osm_pois_a_free_1.shp")) %>%
st_transform(., 27700) %>%
#select hotels only
filter(fclass == 'hotel')
## Reading layer `gis_osm_pois_a_free_1' from data source `/Users/Szymon1/Documents/CASA/Modules/GIS/repo/GIS_code/wk5/prac5_data/greater-london-latest-free/gis_osm_pois_a_free_1.shp' using driver `ESRI Shapefile'
## Simple feature collection with 38148 features and 4 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -0.5108706 ymin: 51.28117 xmax: 0.322123 ymax: 51.68948
## geographic CRS: WGS 84
Londonborough <- st_read(here::here("wk8_2/prac8_2_data",
"statistical-gis-boundaries-london",
"ESRI",
"London_Borough_Excluding_MHW.shp"))%>%
st_transform(., 27700)
## Reading layer `London_Borough_Excluding_MHW' from data source `/Users/Szymon1/Documents/CASA/Modules/GIS/repo/GIS_code/wk8_2/prac8_2_data/statistical-gis-boundaries-london/ESRI/London_Borough_Excluding_MHW.shp' using driver `ESRI Shapefile'
## Simple feature collection with 33 features and 7 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 503568.2 ymin: 155850.8 xmax: 561957.5 ymax: 200933.9
## projected CRS: OSGB 1936 / British National Grid
Airbnb <- read_csv("wk5/prac5_data/listings.csv") %>%
st_as_sf(., coords = c("longitude", "latitude"),
crs = 4326) %>%
st_transform(., 27700)%>%
#select entire places that are available all year
filter(room_type == 'Entire home/apt' & availability_365 =='365')
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## id = col_double(),
## name = col_character(),
## host_id = col_double(),
## host_name = col_character(),
## neighbourhood_group = col_logical(),
## neighbourhood = col_character(),
## latitude = col_double(),
## longitude = col_double(),
## room_type = col_character(),
## price = col_double(),
## minimum_nights = col_double(),
## number_of_reviews = col_double(),
## last_review = col_date(format = ""),
## reviews_per_month = col_double(),
## calculated_host_listings_count = col_double(),
## availability_365 = col_double()
## )
# make a function for the join
# hint all you have to do is replace data1 and data2
# with the data you want to use
Joinfun <- function(data1, data2){
output<- data1%>%
st_join(Londonborough,.)%>%
add_count(GSS_CODE, name="hotels_in_borough")
return(output)
}
# use the function for hotels
Hotels <- Joinfun(OSM, Londonborough)
# then for airbnb
Airbnb <- Joinfun(Airbnb, Londonborough)
# now try to arrange the plots with tmap
breaks = c(0, 5, 12, 26, 57, 286)
#change the column name from hotels_in_borough for the legend
Hotels <- Hotels %>%
dplyr::rename(`Accomodation count` = hotels_in_borough)
Airbnb <- Airbnb %>%
dplyr::rename(`Accomodation count` = hotels_in_borough)
Airbnb_count <- Airbnb %>%
count(GSS_CODE)
Hotels_count <- Hotels %>%
count(NAME)
#join data
Joined <- Airbnb_count%>%
st_join(., Hotels_count, join = st_equals)%>%
st_transform(., 4326)%>%
rename(`Airbnb count` = `n.x`,
`GSS Code`= GSS_CODE,
`Borough`=NAME,
`Hotel count` = `n.y`)
popupairbnb <-Joined %>%
st_drop_geometry()%>%
dplyr::select(`Airbnb count`, Borough)%>%
popupTable()
popuphotel <-Joined %>%
st_drop_geometry()%>%
dplyr::select(`Hotel count`, Borough)%>%
popupTable()
tmap_mode("view")
## tmap mode set to interactive viewing
# set the colour palettes using our previously defined breaks
pal1 <- Joined %>%
colorBin(palette = "YlOrRd", domain=.$`Airbnb count`, bins=breaks)
pal1 <-colorBin(palette = "YlOrRd", domain=Joined$`Airbnb count`, bins=breaks)
pal2 <- Joined %>%
colorBin(palette = "YlOrRd", domain=.$`Hotel count`, bins=breaks)
map<- leaflet(Joined) %>%
# add basemap options
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addProviderTiles(providers$CartoDB.Positron, group = "CartoDB")%>%
#add our polygons, linking to the tables we just made
addPolygons(color="white",
weight = 2,
opacity = 1,
dashArray = "3",
popup = popupairbnb,
fillOpacity = 0.7,
fillColor = ~pal2(`Airbnb count`),
group = "Airbnb")%>%
addPolygons(fillColor = ~pal2(`Hotel count`),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
popup = popupairbnb,
fillOpacity = 0.7,group = "Hotels")%>%
# add a legend
addLegend(pal = pal2, values = ~`Hotel count`, group = c("Airbnb","Hotel"),
position ="bottomleft", title = "Accomodation count") %>%
# specify layers control
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite", "CartoDB"),
overlayGroups = c("Airbnb", "Hotels"),
options = layersControlOptions(collapsed = FALSE)
)
## Warning in pal2(`Airbnb count`): Some values were outside the color scale and
## will be treated as NA
## Warning in pal2(`Hotel count`): Some values were outside the color scale and
## will be treated as NA
# plot the map
map